package yb.ecp.fast.user.web;

import com.github.pagehelper.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.constants.ActionLevel;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.user.infra.BasicController;
import yb.ecp.fast.user.infra.ErrorCode;
import yb.ecp.fast.user.service.UserContextService;
import yb.ecp.fast.user.service.VO.SessionDataVO;

/**
 * Created by john on 2017/11/17.
 */
@RestController
@RequestMapping("/usrCtx")
public class UserContextController extends BasicController
{

    @Autowired
    UserContextService userContextService;

    @RequestMapping(value = "/checkAuthCode", method = RequestMethod.GET)
    @FastMappingInfo(actionLevel = ActionLevel.INNER)
    @ApiOperation(value = "校验用户权限")
    public ActionResult checkAuthCode(@RequestParam("userId") String userId, @RequestParam("authCode")int authCode)
    {
        ErrorCode code = userContextService.checkAuthCode(userId, authCode);
        return actionResult(code);
    }

    @RequestMapping(value = "/session", method = RequestMethod.POST)
    @FastMappingInfo(actionLevel = ActionLevel.INNER)
    @ApiOperation(value = "将用户信息和权限CODE缓存到数据库")
    public ActionResult session(@RequestBody SessionDataVO sessionDataVO)
    {
        userContextService.setSession(sessionDataVO.getUserId(), sessionDataVO.getData(), sessionDataVO.getCodes(),null);
        return actionResult(ErrorCode.Success);
    }

    @RequestMapping(value = "/session", method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "获取缓存中的用户信息与权限CODE")
    public ActionResult session(@RequestHeader(name = "x-user-id", required = false) String userId) {
        if (StringUtil.isEmpty(userId)) {
            return actionResult(ErrorCode.NeedLogin);
        }
        SessionDataVO sessionDataVO = new SessionDataVO();

        Object object = userContextService.getSessionData(userId);
        Integer[] codes = userContextService.getAuthCodes(userId);
        sessionDataVO.setCodes(codes);
        sessionDataVO.setData(object);
        return actionResult(sessionDataVO);
    }

    @RequestMapping(value = "/userData", method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true, actionLevel = ActionLevel.INNER)
    @ApiOperation(value = "获取缓存中的用户信息")
    public ActionResult sessionData(@RequestHeader(name = "x-user-id", required = false) String userId) {
        if (StringUtil.isEmpty(userId)) {
            return actionResult(ErrorCode.NeedLogin);
        }
        Object object = userContextService.getSessionData(userId);
        return actionResult(object);
    }

    @RequestMapping(value = "/authCode", method = RequestMethod.GET)
    @FastMappingInfo(actionLevel = ActionLevel.INNER)
    @ApiOperation(value = "根据用户查询权限CODE")
    public ActionResult authCode(@RequestHeader(name = "x-user-id", required = false) String userId) {
        if (StringUtil.isEmpty(userId)) {
            return actionResult(ErrorCode.NeedLogin);
        }
        Integer[] codes = userContextService.getAuthCodes(userId);
        return actionResult(codes);
    }

    @RequestMapping(value = "/workspaceId", method = RequestMethod.GET)
    @FastMappingInfo(actionLevel = ActionLevel.INNER)
    @ApiOperation(value = "查询用户所在工作空间Id")
    public ActionResult<String> getWorkspaceId(@RequestParam("userId") String userId)
    {
        String workspaceId = userContextService.getWorkspaceId(userId);
        return actionResult(workspaceId);
    }
}
